package com.pl.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.SysUser;
import com.pl.dao.SysUserDao;
import com.pl.lister.SysUserReadLister;
import com.pl.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2024-04-24 17:26:06
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUser queryById(Long userId) {
        return this.sysUserDao.queryById(userId);
    }


    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(SysUser sysUser, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        sysUser.setCreateBy(user.getUserName());
        sysUser.setCreateTime(new Date());
        int num = this.sysUserDao.insert(sysUser);
        if (num > 0) {
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(int userId) {
        return this.sysUserDao.deleteById(userId) > 0;
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @Override
    public boolean deleteBatch(List<Integer> list) {
        int i = sysUserDao.deleteBatch(list);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 批量插入，用于文件导入时 配合监听器使用
     *
     * @param sysUserList
     * @return
     */
    @Override
    public boolean inserts(List<SysUser> sysUserList) {
        int num = sysUserDao.inserts(sysUserList);
        if (num > 0) {
            return true;
        }
        return false;
    }

    /**
     * 导入文件
     *
     * @param file
     * @return
     */
    @Override
    public String importSysUserFile(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SysUser.class, new SysUserReadLister(this)).sheet("用户信息").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }

    /**
     * 导出文件
     *
     * @param response
     */
    @Override
    public void exportSysUserFile(HttpServletResponse response) {
        List<SysUser> sysUserList = sysUserDao.finAll();
        try {
            EasyExcel.write(response.getOutputStream(), SysUser.class).sheet("用户信息").doWrite(sysUserList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @Override
    public boolean finAll() {
        List<SysUser> users = sysUserDao.finAll();
        if (users != null) {
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(SysUser sysUser) {
        int num = this.sysUserDao.update(sysUser);
        if (num > 0) {
            return true;
        }
        return false;
    }


    /**
     * 分页查询列表数据 以及模糊搜索
     *
     * @param map
     * @return
     */
    @Override
    public List<SysUser> queryByPage(Map<String, Object> map) {
        return sysUserDao.queryByPage(map);
    }

    /**
     * 登录操作
     *
     * @param sysUser
     * @return
     */
    @Override
    public SysUser login(SysUser sysUser, HttpServletRequest request, HttpSession session) {
        // 1.先查找用户，以键值对的方式存在
        //存在即可登陆成功
        SysUser user = sysUserDao.login(sysUser);

        //如果user存在 需要更改数据库中的登录时间和ip地址
        if (user != null) {
            user.setLoginIp(request.getRemoteHost());
            user.setLoginDate(new Date());
            //将user的数据 账号名和密码存储到session中
            session.setAttribute("user", user);
            //更新数据库，
            sysUserDao.update(user);
            return user;
        }
        return null;
    }


}
